package authentication.module;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;

public class UserLoginModule implements LoginModule {
	private CallbackHandler userCallbackHandler = null;
	private boolean userAuthenticationSuccessFlag = false;

	@Override
	public boolean abort() throws LoginException {
		return false;
	}

	@Override
	public boolean commit() throws LoginException {
		return userAuthenticationSuccessFlag;
	}

	@Override
	public void initialize(Subject arg0, CallbackHandler userCallbackHandler,
			Map<String, ?> arg2, Map<String, ?> arg3) {
		this.userCallbackHandler = userCallbackHandler;
	}

	@Override
	public boolean login() throws LoginException {
		Callback[] userCallbackArray = new Callback[2];
		userCallbackArray[0] = new NameCallback("User name:");
		userCallbackArray[1] = new PasswordCallback("Password:", false);
		try {
			userCallbackHandler.handle(userCallbackArray);
		} catch (IOException | UnsupportedCallbackException e) {
			e.printStackTrace();
		}
		// the user's input (credentials) is stored in the "name" and "password"
		// variables
		String name = ((NameCallback) userCallbackArray[0]).getName();
		String password = new String(
				((PasswordCallback) userCallbackArray[1]).getPassword());
		// reading the stored user's credentials from a file for a later
		// comparison
		File file = new File(
				"C:/Users/Siji/workspace/PsiExam-Authentication/credentials/DB/psychologistCredentials.txt");
		try {
			Scanner scanner = new Scanner(file);
			List<String> nameList = new ArrayList<String>();
			List<String> passwordList = new ArrayList<String>();
			String credentialsLine = null;
			// separate lists are created for stored usernames, respectively
			// stored passwords
			while (scanner.hasNext()) {
				credentialsLine = scanner.nextLine();
				String[] credentialParts = credentialsLine.split("/");
				nameList.add(credentialParts[0]);
				passwordList.add(credentialParts[1]);
			}
			scanner.close();
			// user input is compared with each username line in the stored
			// username list respectively the same for passwords
			for (int i = 0; i <= nameList.size();) {
				if (nameList.get(i).equals(name)
						&& passwordList.get(i).equals(password)) {
					System.out
							.println("the authentication was a success!!! :)");
					userAuthenticationSuccessFlag = true;
					break;
				} else {
					i++;
					if (i == nameList.size()) {
						userAuthenticationSuccessFlag = false;
						throw new FailedLoginException(
								"the authentication was a failure!!! :(");
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return userAuthenticationSuccessFlag;
	}

	@Override
	public boolean logout() throws LoginException {
		return false;
	}

}
